<?php

namespace App\Services;

use App\Models\Escort;
use App\Models\User;
use App\Models\UserTokens;
use Illuminate\Database\Eloquent\Model;

/**
 * 前端用户服务
 * Class UserService
 * @package App\Services
 */
class UserService extends BaseService
{

    /**
     * @param $userinfo
     * @return array
     * 添加用户
     */
    public function addUserInfo($userinfo)
    {

        $userModel = User::where('wxapp_openid', trim($userinfo['wxapp_openid']))->first();
        if (!$userModel) {
            $userModel = new User();
            $userModel->unionid = isset($userinfo['unionid']) ? $userinfo['unionid'] : 0;
            $userModel->openid = $userinfo['openId'] ? $userinfo['openId'] : '';
            $userModel->wxapp_openid = $userinfo['wxapp_openid'] ? $userinfo['wxapp_openid'] : '';
            $userModel->nickname = $userinfo['nickName'] ? $userinfo['nickName'] : '';
            $userModel->balance = 0;
            $userModel->avatar = isset($userinfo['avatarUrl']) ? $userinfo['avatarUrl'] : '';
            $userModel->mobile = '';
            $userModel->gender = isset($userinfo['gender']) ? $userinfo['gender'] : '';
            $userModel->province = isset($userinfo['province']) ? $userinfo['province'] : '';
            $userModel->country = isset($userinfo['country']) ? $userinfo['country'] : '';
            $userModel->city = isset($userinfo['city']) ? $userinfo['city'] : '';
            $userModel->ip = CLIENT_IP;
            $userModel->state = 1;
            $userModel->save();
        }
        $token = $this->updateAccessToken($userModel);

        $userModel->is_escort = -1;
        $userModel->is_work=0;

        $escort = Escort::whereUid($userModel->id)->first();
        $this->escort_id = $escort->id?:0;
        if($escort && $escort->status == 0){
            $userModel->is_escort = 0;
        }elseif($escort->status > 0){
            $userModel->is_escort = 1;
            $userModel->is_work = $escort->status==1?1:0;
        }

        return [
            'cache_key' => $token->cache_key,
            'expires_time' => $token->expires_time,
            'token' => $token->access_token,
            'userInfo' => $userModel
        ];
    }

    public function updateAccessToken($user)
    {
        $key = mt_rand();
        $hash = md5($key . $user->wxapp_openid . mt_rand() . time() . $user->id);
        $token_access = base64_encode($hash);
        $TokenModel = new UserTokens();
        $TokenModel->access_token = $token_access;
        $TokenModel->expires_time = time() + UserTokens::$expires_time;
        $TokenModel->login_ip = CLIENT_IP;
        $TokenModel->uid = $user->id;
        $TokenModel->save();
        $TokenModel->cache_key = md5($hash);
        return $TokenModel;
    }

    /**
     * 添加用户
     * @param $userInfo
     * @return User|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|object|null
     */
    public static function addUser($userInfo)
    {
        $userModel = User::whereMobile(trim($userInfo['mobile']))->first();

        if (!$userModel) {
            $userModel = new User();
            $userInfo['nickname'] = self::generateNickname();
        }
        $userModel->fill($userInfo)->save();

        return $userModel;
    }

    /**
     * 生成昵称
     */
    public static function generateNickname()
    {
        return 'dgl'.random(8, true);
    }
}
